package com.example.crossborderecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crossborderecommerce.entity.*;
import com.example.crossborderecommerce.service.ICurrencyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  汇率模块
 *
 * @author Yolanda
 * @since 2024-05-09
 */
@RestController
@Transactional
@RequestMapping("/currency")
public class CurrencyController {
    @Resource
    ICurrencyService currencyService;

    /**
     * 保存货币
     * @param currency
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Currency currency){
        boolean res = currencyService.save(currency);
        if(res){
            return Result.success("添加货币成功！");
        }
        return Result.error("添加货币失败！");
    }

    /**
     *删除某个货币
     * @param currency
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Currency currency) {
        boolean res = currencyService.removeById(currency);
        if(res) {
            return Result.success("删除货币成功！");
        }
        return Result.error("删除货币失败！");
    }

    /**
     * 删除一系列货币
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam String ids) {
        List<String> currencyIds = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        boolean res = currencyService.removeByIds(currencyIds);
        if(res) {
            return Result.success("删除货币成功！");
        }
        return Result.error("删除货币失败！");
    }

    /**
     * 修改货币
     * @param currency
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Currency currency) {
        boolean res = currencyService.saveOrUpdate(currency);
        if (res) {
            return Result.success("修改货币成功！");
        }
        return Result.error("修改货币失败！");
    }

    /**
     * 分页查询所有的货币
     * @param nameBefore
     * @param nameAfter
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "") String nameBefore,
                           @RequestParam(defaultValue = "") String nameAfter,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Currency> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name_before", nameBefore);
        queryWrapper.like("name_after", nameAfter);
        Page<Currency> adminPage = currencyService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success("成功查询所有货币信息！",adminPage);
    }

    /**
     * 以列表形式返回所有货币
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Currency> currencyList = currencyService.list();
        if(currencyList != null) {
            return Result.success("查询所有货币成功！", currencyList);
        }
        return Result.error("查询所有货币失败！");
    }
}
